package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.constant.AppConstant;
import io.renren.entity.AppUserEntity;
import io.renren.entity.PointsResult;
import io.renren.po.UserPoints;
import io.renren.service.PersonalCenterService;
import io.renren.vo.RespModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by ThinkPad on 2018/11/29.
 */
@RestController
@RequestMapping("/personalCenter")
@Api(tags = "个人中心")
public class PersonalCenterController {
    private Logger logger = LoggerFactory.getLogger(PersonalCenterController.class);

    @Autowired
    private PersonalCenterService personalCenterService;

    @PostMapping(value = "/getUserPoints/{userid}",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="查询用户积分",produces = MediaType.APPLICATION_JSON_VALUE)
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel getUserPoints(@PathVariable String userid) {
        if (StringUtils.isEmpty(userid)) {
            return new RespModel<>().getRespModelInstance(AppConstant.PARAM_IS_MISS, "用户标识不能为空", null);
        }
        PointsResult pointsResult = personalCenterService.queryUserPoints(userid);
        return new RespModel().getRespModelInstance(AppConstant.STATU_SUCCESS, "", pointsResult);
    }

    @GetMapping(value = "/getGoodsList",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="查询积分商品")
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel getGoodsList(){
        List<Map> goods = personalCenterService.queryGoods();
        return new RespModel<>().getRespModelInstance(AppConstant.STATU_SUCCESS, "", goods);
    }

    @PostMapping(value = "/getPointsList/{userid}",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="查询用户积分记录",produces = MediaType.APPLICATION_JSON_VALUE)
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel getPointsLogs(@PathVariable String userid){
        if (StringUtils.isEmpty(userid)) {
            return new RespModel<>().getRespModelInstance(AppConstant.PARAM_IS_MISS, "用户标识不能为空", null);
        }
        List<Map> maps = personalCenterService.queryPoints(userid);
        return new RespModel().getRespModelInstance(AppConstant.STATU_SUCCESS, "", maps);
    }

    @PostMapping(value = "/operatePoints", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "操作用户积分", produces = MediaType.APPLICATION_JSON_VALUE)
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel operateUserPoints(@Valid UserPoints userPoints, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            StringBuilder sb = new StringBuilder();
            bindingResult.getFieldErrors().forEach(filederror ->
                    sb.append(filederror.getDefaultMessage()).append(";")
            );
           return new RespModel<>().getRespModelInstance(AppConstant.PARAM_IS_MISS, sb.toString(), null);
        }

        return personalCenterService.updateUserPointByOperation(userPoints);
    }


    @PostMapping(value="upgradeVip",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="用户升级vip",produces = MediaType.APPLICATION_JSON_VALUE)
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel upgradeVip(@Valid AppUserEntity appUserEntity, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            StringBuilder sb = new StringBuilder();
            bindingResult.getFieldErrors().forEach(fieldError -> sb.append(fieldError.getDefaultMessage()).append(";"));
            return new RespModel().getRespModelInstance(AppConstant.PARAM_IS_MISS, sb.toString(), null);
        }
        return personalCenterService.upgradeVIp(appUserEntity);
    }

    @Login
    @PostMapping(value="/queryUserLevel/{applicationId}",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="查询用户等级",produces = MediaType.APPLICATION_JSON_VALUE)
    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel queryUserLevel(HttpServletRequest request) {
        String userid = (String) request.getAttribute("username");
        Map map = personalCenterService.queryAppUserInfo(userid);
        return new RespModel().getRespModelInstance(AppConstant.STATU_SUCCESS, "", map);
    }

    @Login
    @GetMapping(value = "/getUserInfo/{applicationId}",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="查询VIP用户信息",produces = MediaType.APPLICATION_JSON_VALUE)
//    @com.cxytiandi.encrypt.springboot.annotation.Decrypt
    @com.cxytiandi.encrypt.springboot.annotation.Encrypt
    public RespModel getUserInfo(HttpServletRequest request) {
        String userid = (String) request.getAttribute("username");
        Map map = personalCenterService.queryAppUserInfo(userid);
        return new RespModel().getRespModelInstance(AppConstant.STATU_SUCCESS, "", map);
    }
}
